iris
iris
summary()summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width ## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 ## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 ## Median :5.800 Median :3.000 Median :4.350 Median :1.300 ## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 ## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 ## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 ## Species ## setosa :50 ## versicolor:50 ## virginica :50 ## ## ##
Oltre la media, abbiamo bisogno di una misura di quanto i dati sono dispersi
Se \(\overline x\) è la media aritmetica del mio campione:
la varianza campionaria sarà :
\[ \sigma^2 = {\frac{\sum(x-\overline x)^2}{N-1}} \]
e la deviazione standard campionaria o scarto quadratico medio campionario
\[ \sigma = \sqrt{\frac{\sum(x-\overline x)^2}{N-1}} \]
iris$Petal.Length
## [1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 1.5 1.6 1.4 1.1 1.2 1.5 1.3 ## [18] 1.4 1.7 1.5 1.7 1.5 1.0 1.7 1.9 1.6 1.6 1.5 1.4 1.6 1.6 1.5 1.5 1.4 ## [35] 1.5 1.2 1.3 1.4 1.3 1.5 1.3 1.3 1.3 1.6 1.9 1.4 1.6 1.4 1.5 1.4 4.7 ## [52] 4.5 4.9 4.0 4.6 4.5 4.7 3.3 4.6 3.9 3.5 4.2 4.0 4.7 3.6 4.4 4.5 4.1 ## [69] 4.5 3.9 4.8 4.0 4.9 4.7 4.3 4.4 4.8 5.0 4.5 3.5 3.8 3.7 3.9 5.1 4.5 ## [86] 4.5 4.7 4.4 4.1 4.0 4.4 4.6 4.0 3.3 4.2 4.2 4.2 4.3 3.0 4.1 6.0 5.1 ## [103] 5.9 5.6 5.8 6.6 4.5 6.3 5.8 6.1 5.1 5.3 5.5 5.0 5.1 5.3 5.5 6.7 6.9 ## [120] 5.0 5.7 4.9 6.7 4.9 5.7 6.0 4.8 4.9 5.6 5.8 6.1 6.4 5.6 5.1 5.6 6.1 ## [137] 5.6 5.5 4.8 5.4 5.6 5.1 5.1 5.9 5.7 5.2 5.0 5.2 5.4 5.1
ggplot() +
geom_density(data = iris, aes(x = Petal.Length, fill = Species),
binwidth = 0.1, color = "black", alpha = 2/3) +
theme_bw()
La funzione per il calcolo della media è mean()
mean(iris$Sepal.Length)
## [1] 5.843333
Se il nostro dataframe ha una colonna factor, possiamo raggupparlo con:
iris %>%
group_by(Species) %>%
summarise(mean_values = mean(Petal.Length))
In inglese è standard deviation, per cui la funzione è sd()
sd(iris$Petal.Length)
## [1] 1.765298
Come tutte le funzioni può essere applicata a gruppi:
iris %>%
group_by(Species) %>%
summarise(mean_values = mean(Petal.Length), standard_deviations = sd(Petal.Length))
Lo salviamo in un dataframe
iris %>%
group_by(Species) %>%
summarise(mean_values = mean(Petal.Length),
standard_deviations = sd(Petal.Length)) -> iris_stat
ggplot() +
geom_density(data = iris, aes(x = Petal.Length, fill = Species),
binwidth = 0.1, color = "black", alpha = 2/3) +
geom_vline(data = iris_stat,
aes(xintercept = mean_values, color = Species), size = 1.5) +
geom_vline(data = iris_stat,
aes(xintercept = mean_values + standard_deviations, color = Species), size = 0.7) +
geom_vline(data = iris_stat,
aes(xintercept = mean_values - standard_deviations, color = Species), size = 0.7) +
theme_bw()
%>% (piping)+ di ggplot2a %>% fun1() %>% fun2(arg2, arg3)
corrisponde a:
fun2(fun1(a), arg2, arg3)
a %>% fun1() %>% fun2(arg2, arg3)
si appoggia ad una variabile transitoria
k <- fun1(a) k <- fun2(k, arg2, arg3) k
spesso (e soprattutto in questo caso) per le variabili transitorie si usa il punto
. <- fun1(a) . <- fun2(. , arg2, arg3) .
per un approfondimento (e un possibile argomento per un tutorial)
help("%>%")
library(GGally) ggpairs(iris, binwidth = 0.1) + theme_bw()
ggpairs(iris, columns = c("Petal.Length", "Petal.Width", "Species"),
mapping = aes(alpha = 0.20)) + theme_bw()
ggpairs(iris,
mapping = aes(alpha = 0.20, color = Species)) + theme_bw()
ggpairs(iris, columns = c("Petal.Length", "Petal.Width", "Species"),
mapping = aes(alpha = 0.20, color = Species)) + theme_bw()
library(corrplot) cor_data <- cor(dplyr::select_if(iris, is.numeric)) corrplot.mixed(cor_data)
corrplot.mixed(cor_data, lower = "number", upper = "square", lower.col = "black")
Esempi di valori di correlazione
Iniziamo da una variabile sola
creiamo una variabile da una distribuzione normale di media 5, la funzione che estrae i valori da una distribuzione uniforme era runif(), quella per la distribuzione normale è rnorm().
Non scordiamoci il set.seed()
set.seed(42) data <- data_frame(x = 1:10, y = rnorm(10, mean = 5))
data
ggplot() +
geom_point(data = data, aes(x = x, y = y), color = "purple", size = 3) +
theme_bw()
mean(data$y)
## [1] 5.547297
lm(y ~ 1, data = data)
## ## Call: ## lm(formula = y ~ 1, data = data) ## ## Coefficients: ## (Intercept) ## 5.547
ggplot(lm(y ~ 1, data = data)) +
geom_point(aes(x = data$x, y = .resid), size = 3, color = "purple") +
theme_bw()
summary(anscombe)
## x1 x2 x3 x4 ## Min. : 4.0 Min. : 4.0 Min. : 4.0 Min. : 8 ## 1st Qu.: 6.5 1st Qu.: 6.5 1st Qu.: 6.5 1st Qu.: 8 ## Median : 9.0 Median : 9.0 Median : 9.0 Median : 8 ## Mean : 9.0 Mean : 9.0 Mean : 9.0 Mean : 9 ## 3rd Qu.:11.5 3rd Qu.:11.5 3rd Qu.:11.5 3rd Qu.: 8 ## Max. :14.0 Max. :14.0 Max. :14.0 Max. :19 ## y1 y2 y3 y4 ## Min. : 4.260 Min. :3.100 Min. : 5.39 Min. : 5.250 ## 1st Qu.: 6.315 1st Qu.:6.695 1st Qu.: 6.25 1st Qu.: 6.170 ## Median : 7.580 Median :8.140 Median : 7.11 Median : 7.040 ## Mean : 7.501 Mean :7.501 Mean : 7.50 Mean : 7.501 ## 3rd Qu.: 8.570 3rd Qu.:8.950 3rd Qu.: 7.98 3rd Qu.: 8.190 ## Max. :10.840 Max. :9.260 Max. :12.74 Max. :12.500
corrplot.mixed(cor(anscombe))